package com.Set;

import java.util.Comparator;
import java.util.Objects;

public class Student implements Comparable<Student> {
    private String name;
    private int age;
    private  char sex;

    public Student() {
    }

    public Student(String name, int age, char sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }


    /**
     * equal方法默认比较的是对象的地址
     * 重写equal方法：比较对象的内容，而不是比较地址，只要内容一样，就判定这两个对象一样
     * @param o
     * @return
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                sex == student.sex &&
                Objects.equals(name, student.name);
    }

    /**
     * hascode方法默认传入的是对象的地址，我们把对象内容作为参数
     * 重写hascode方法，只要两个对象的内容一样，就返回相同的哈希值
     * 重写来了hashcode方法，我们王HashSet里面添加元素的时候就不会重复了
     * @return
     */
    @Override
    public int hashCode() {
        return Objects.hash(name, age, sex);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //this表示当前对象
        return this.getAge()-o.getAge();
    }
}
